package com.yinda.dt.produce.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.yinda.dt.produce.R;
import com.yinda.dt.produce.app.DemoActivity;
import com.yinda.dt.produce.app.DemoConfig;
import com.yinda.dt.produce.app.MyResponseCallback;
import com.yinda.dt.produce.module.main.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class LoginActivity extends DemoActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private Button b_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        b_login = (Button) findViewById(R.id.b_login);
        b_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.b_login) {
            if (et_username.getText().toString().trim().isEmpty()) {
                et_username.setError("不能为空");
                return;
            }
            if (et_password.getText().toString().trim().isEmpty()) {
                et_password.setError("不能为空");
                return;
            }
            login();
        }
    }

    private void login() {
        try {
            String url = getString(R.string.defaultURL) + "/phone1_0!userLogin.action";
            JSONObject jsonParams = new JSONObject();
            jsonParams.put("username", et_username.getText().toString().trim());
            jsonParams.put("password", et_password.getText().toString().trim());
            post(url, jsonParams, new MyResponseCallback(loadingDialog) {
                @Override
                public void onSuccess(JSONObject jsonResult) {
                    super.onSuccess(jsonResult);

                    try {
                        JSONObject jsonHead = jsonResult.getJSONObject("head");
                        int status = jsonHead.getInt("status");
                        String info = jsonHead.getString("info");
                        if (status == 1) {
                            JSONObject jsonData = jsonResult.getJSONObject("data");
                            String isPm = jsonData.getString("isPm");
                            String key = jsonData.getString("key");
                            String uid = jsonData.getString("uid");
                            String account = jsonData.getString("accout");
                            String name = jsonData.getString("name");
                            String tel = jsonData.getString("tel");
                            String userimgUrl = jsonData.getString("portrait");
                            DemoConfig.getInstance(getContext()).setKey(key);
                            DemoConfig.getInstance(getContext()).setUid(uid);
                            DemoConfig.getInstance(getContext()).setAccout(account);
                            DemoConfig.getInstance(getContext()).setName(name);
                            DemoConfig.getInstance(getContext()).setTel(tel);
                            DemoConfig.getInstance(getContext()).setUserimgUrl(userimgUrl);
                            DemoConfig.getInstance(getContext()).setIsPm(isPm);

                            DemoConfig.getInstance(getContext()).setIsLogin(true);

                            Intent intent = new Intent(getContext(), MainActivity.class);
                            startActivity(intent);
                            finish();

                        } else {
                            showToast(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
